﻿using System;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using Yoozoo.Managers.ResourceManagerV2.Editor.ResourceTools;

namespace com.yoozoo.gta.Extension.UGUI.Editor
{
    
    public class ScreenOrientUIImporter : AssetPostprocessor
    {
            
        private const string UIRootPath = "Assets/ResourcesAssets/Prefabs/UI";
        private const string ResourceUIRootPath = "Assets/ResourcesAssets/Prefabs/UI";
            //当资源被导入时调用

            private void OnPostprocessPrefab(GameObject g)
            {
                
                var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
                if(prefabStage != null && prefabStage.assetPath == this.assetPath)
                {
                    return;
                }

                if (assetPath.Contains(UIRootPath) || assetPath.Contains(ResourceUIRootPath))
                {
                    Debug.Log($"OnPostprocessPrefab: {this.assetPath}  ");
                    var items = g.GetComponentsInChildren<ScreenOrientItem>();
                    foreach (var item in items)
                    {
                        item.CleanUpAdapter();
                        item.ResetToLandscape(); //TODO:这里的导入修改会因为：Importer(PrefabImporter) generated inconsistent result for asset 不会生效
                        Debug.LogFormat("OnPostprocessPrefab: {0}  ResetToLandscape", item.gameObject.name);
                    }
                }
                AssetDatabase.SaveAssetIfDirty(g);
                
            }
    }
}